{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import arcpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 这个方法是把arcpy的线对象转换成folium的线对象\n",
    "import json,folium\n",
    "def aLineTofLine(arcpyLine:arcpy.Polyline,color=\"red\"):\n",
    "    coord = []\n",
    "    for r in json.loads(arcpyLine.JSON)[\"paths\"]:\n",
    "        c = []\n",
    "        for r2 in r:\n",
    "            c.append([r2[1],r2[0]])\n",
    "        coord.append(c)\n",
    "    return folium.PolyLine(locations=coord,color=color)\n",
    "\n",
    "# GeoQ地图灰色版\n",
    "tile=\"http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetGray/MapServer/tile/{z}/{y}/{x}\"\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "sr = arcpy.SpatialReference(4326)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "sd = arcpy.Point(116.5930544,40.0795454)\n",
    "dx = arcpy.Point(116.4168081, 39.5129736)\n",
    "line = arcpy.Polyline(inputs=arcpy.Array([sd,dx]),\n",
    "spatial_reference=sr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "pt_tam = arcpy.PointGeometry(inputs=arcpy.Point(116.396885,39.907713),\n",
    "spatial_reference=sr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "pt2 = arcpy.PointGeometry(inputs=arcpy.Point(116.796885,39.607713),\n",
    "spatial_reference=sr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><span style=\"color:#565656\">Make this Notebook Trusted to load map: File -> Trust Notebook</span><iframe srcdoc=\"&lt;!DOCTYPE html&gt;\n",
       "&lt;head&gt;    \n",
       "    &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;\n",
       "    \n",
       "        &lt;script&gt;\n",
       "            L_NO_TOUCH = false;\n",
       "            L_DISABLE_3D = false;\n",
       "        &lt;/script&gt;\n",
       "    \n",
       "    &lt;style&gt;html, body {width: 100%;height: 100%;margin: 0;padding: 0;}&lt;/style&gt;\n",
       "    &lt;style&gt;#map {position:absolute;top:0;bottom:0;right:0;left:0;}&lt;/style&gt;\n",
       "    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/leaflet@1.6.0/dist/leaflet.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;script src=&quot;https://code.jquery.com/jquery-1.12.4.min.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/leaflet@1.6.0/dist/leaflet.css&quot;/&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css&quot;/&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css&quot;/&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css&quot;/&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css&quot;/&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/gh/python-visualization/folium/folium/templates/leaflet.awesome.rotate.min.css&quot;/&gt;\n",
       "    \n",
       "            &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width,\n",
       "                initial-scale=1.0, maximum-scale=1.0, user-scalable=no&quot; /&gt;\n",
       "            &lt;style&gt;\n",
       "                #map_7ba4245973ed28051d3a3939402708c0 {\n",
       "                    position: relative;\n",
       "                    width: 100.0%;\n",
       "                    height: 100.0%;\n",
       "                    left: 0.0%;\n",
       "                    top: 0.0%;\n",
       "                }\n",
       "            &lt;/style&gt;\n",
       "        \n",
       "&lt;/head&gt;\n",
       "&lt;body&gt;    \n",
       "    \n",
       "            &lt;div class=&quot;folium-map&quot; id=&quot;map_7ba4245973ed28051d3a3939402708c0&quot; &gt;&lt;/div&gt;\n",
       "        \n",
       "&lt;/body&gt;\n",
       "&lt;script&gt;    \n",
       "    \n",
       "            var map_7ba4245973ed28051d3a3939402708c0 = L.map(\n",
       "                &quot;map_7ba4245973ed28051d3a3939402708c0&quot;,\n",
       "                {\n",
       "                    center: [39.9, 116.39],\n",
       "                    crs: L.CRS.EPSG3857,\n",
       "                    zoom: 9,\n",
       "                    zoomControl: true,\n",
       "                    preferCanvas: false,\n",
       "                }\n",
       "            );\n",
       "\n",
       "            \n",
       "\n",
       "        \n",
       "    \n",
       "            var tile_layer_039176cb6447be894f2b977eae1391fb = L.tileLayer(\n",
       "                &quot;http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetGray/MapServer/tile/{z}/{y}/{x}&quot;,\n",
       "                {&quot;attribution&quot;: &quot;geoq&quot;, &quot;detectRetina&quot;: false, &quot;maxNativeZoom&quot;: 18, &quot;maxZoom&quot;: 18, &quot;minZoom&quot;: 0, &quot;noWrap&quot;: false, &quot;opacity&quot;: 1, &quot;subdomains&quot;: &quot;abc&quot;, &quot;tms&quot;: false}\n",
       "            ).addTo(map_7ba4245973ed28051d3a3939402708c0);\n",
       "        \n",
       "    \n",
       "            var poly_line_7bcd7db1ac61087f9561da4eae82ea38 = L.polyline(\n",
       "                [[[40.0795454, 116.5930544], [39.5129736, 116.4168081]]],\n",
       "                {&quot;bubblingMouseEvents&quot;: true, &quot;color&quot;: &quot;red&quot;, &quot;dashArray&quot;: null, &quot;dashOffset&quot;: null, &quot;fill&quot;: false, &quot;fillColor&quot;: &quot;red&quot;, &quot;fillOpacity&quot;: 0.2, &quot;fillRule&quot;: &quot;evenodd&quot;, &quot;lineCap&quot;: &quot;round&quot;, &quot;lineJoin&quot;: &quot;round&quot;, &quot;noClip&quot;: false, &quot;opacity&quot;: 1.0, &quot;smoothFactor&quot;: 1.0, &quot;stroke&quot;: true, &quot;weight&quot;: 3}\n",
       "            ).addTo(map_7ba4245973ed28051d3a3939402708c0);\n",
       "        \n",
       "    \n",
       "            var marker_e42648d872033c0182c5c79a291fab5c = L.marker(\n",
       "                [39.907713, 116.396885],\n",
       "                {}\n",
       "            ).addTo(map_7ba4245973ed28051d3a3939402708c0);\n",
       "        \n",
       "    \n",
       "            var icon_beab841efc54f625d2703a4c37561a64 = L.AwesomeMarkers.icon(\n",
       "                {&quot;extraClasses&quot;: &quot;fa-rotate-0&quot;, &quot;icon&quot;: &quot;info-sign&quot;, &quot;iconColor&quot;: &quot;white&quot;, &quot;markerColor&quot;: &quot;green&quot;, &quot;prefix&quot;: &quot;glyphicon&quot;}\n",
       "            );\n",
       "            marker_e42648d872033c0182c5c79a291fab5c.setIcon(icon_beab841efc54f625d2703a4c37561a64);\n",
       "        \n",
       "    \n",
       "        var popup_78c9f11db58d6e87f60e4bde04b9d235 = L.popup({&quot;maxWidth&quot;: &quot;100%&quot;});\n",
       "\n",
       "        \n",
       "            var html_3864486e4940b4f6eec4d711f207717b = $(`&lt;div id=&quot;html_3864486e4940b4f6eec4d711f207717b&quot; style=&quot;width: 100.0%; height: 100.0%;&quot;&gt;&lt;/div&gt;`)[0];\n",
       "            popup_78c9f11db58d6e87f60e4bde04b9d235.setContent(html_3864486e4940b4f6eec4d711f207717b);\n",
       "        \n",
       "\n",
       "        marker_e42648d872033c0182c5c79a291fab5c.bindPopup(popup_78c9f11db58d6e87f60e4bde04b9d235)\n",
       "        ;\n",
       "\n",
       "        \n",
       "    \n",
       "    \n",
       "            var marker_3f8ce8dae77802017411a46b1ea7d2c3 = L.marker(\n",
       "                [39.607713, 116.796885],\n",
       "                {}\n",
       "            ).addTo(map_7ba4245973ed28051d3a3939402708c0);\n",
       "        \n",
       "    \n",
       "            var icon_3f63f94b73b5e07c25289f55ba2e6db1 = L.AwesomeMarkers.icon(\n",
       "                {&quot;extraClasses&quot;: &quot;fa-rotate-0&quot;, &quot;icon&quot;: &quot;info-sign&quot;, &quot;iconColor&quot;: &quot;white&quot;, &quot;markerColor&quot;: &quot;blue&quot;, &quot;prefix&quot;: &quot;glyphicon&quot;}\n",
       "            );\n",
       "            marker_3f8ce8dae77802017411a46b1ea7d2c3.setIcon(icon_3f63f94b73b5e07c25289f55ba2e6db1);\n",
       "        \n",
       "    \n",
       "        var popup_847cee3df43a0b6dba5de1e0207ea10c = L.popup({&quot;maxWidth&quot;: &quot;100%&quot;});\n",
       "\n",
       "        \n",
       "            var html_b62eb8b6d88b78816963a7aa277c81a9 = $(`&lt;div id=&quot;html_b62eb8b6d88b78816963a7aa277c81a9&quot; style=&quot;width: 100.0%; height: 100.0%;&quot;&gt;&lt;/div&gt;`)[0];\n",
       "            popup_847cee3df43a0b6dba5de1e0207ea10c.setContent(html_b62eb8b6d88b78816963a7aa277c81a9);\n",
       "        \n",
       "\n",
       "        marker_3f8ce8dae77802017411a46b1ea7d2c3.bindPopup(popup_847cee3df43a0b6dba5de1e0207ea10c)\n",
       "        ;\n",
       "\n",
       "        \n",
       "    \n",
       "&lt;/script&gt;\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
      ],
      "text/plain": [
       "<folium.folium.Map at 0x2725cc5bb50>"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = folium.Map([39.9, 116.39],\n",
    "               tiles=tile,attr='geoq',\n",
    "               zoom_start=9)\n",
    "aLineTofLine(line).add_to(m)\n",
    "folium.Marker(\n",
    "    [pt_tam.centroid.Y, pt_tam.centroid.X],\n",
    "     popup=\"\", icon=folium.Icon(color=\"green\", icon=\"info-sign\")\n",
    ").add_to(m)\n",
    "folium.Marker(\n",
    "    [pt2.centroid.Y, pt2.centroid.X],\n",
    "     popup=\"\", icon=folium.Icon(color=\"blue\", icon=\"info-sign\")\n",
    ").add_to(m)\n",
    "\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<PointGeometry object at 0x2725c2e77c0[0x2725c2e7750]>,\n",
       " 0.2223462072453054,\n",
       " 0.13627535107182545,\n",
       " True)"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qpt = line.queryPointAndDistance(pt_tam)\n",
    "qpt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<PointGeometry object at 0x2725c2e7790[0x2725c2e7810]>,\n",
       " 0.38999214579103425,\n",
       " 0.33478179969559413,\n",
       " False)"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qpt2 = line.queryPointAndDistance(pt2)\n",
    "qpt2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.11 ('ProDev')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.11"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "06b308fdbfd5979111721ebbf4de1ae2cbf405c7740d4ab5c57cb4c3e518bed6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
